<?php
	class AdmTypeEtablissement extends AdmModule {
		public function action_index() {
			if (isset($this->session->modification_type_etablissement_id)) {
				unset($this->session->modification_type_etablissement_id);
			}
		
			$this->set_title("Liste des type d'établissement possibles");
			$this->tpl->assign('liste', TypeEtablissement::recupererListe());
		}
		
		public function action_ajouter() {
			$formulaire = new Form("?module=admtypeetablissement&action=valider_ajout","formulaire");
			$formulaire->add_text("code_type_etablissement","code_type_etablissement","Code du type :");
			$formulaire->add_text("type_etablissement","type_etablissement","Détails sur le type :");
			$formulaire->add_submit("ajouter_type_etablissement_submit", "ajouter_type_etablissement_submit")->set_value("Ajouter le type");	
			
			
			$this->tpl->assign("formulaire", $formulaire);		
		}
		
		public function action_valider_ajout() {
			if ($this->req->type_etablissement == '') {
				$this->site->ajouter_message("Veuillez indiquer le type.");
				$this->site->redirect('admtypeetablissement', 'ajouter');
			}
			elseif ($this->req->code_type_etablissement == '') {
				$this->site->ajouter_message("Veuillez indiquer le code du type.");
				$this->site->redirect('admtypeetablissement', 'ajouter');
			}
			else {
				$type_etablissement = new TypeEtablissement($this->req->type_etablissement);
				$type_etablissement->identifiant = $this->req->code_type_etablissement;
				$type_etablissement->enregistrer();
				
				$this->site->redirect('admtypeetablissement');
			}
		}
		
		public function action_modifier() {
			if ($this->req->id != '' or $this->session->modification_type_etablissement_id != '') {
				try {
					$type_etablissement = TypeEtablissement::recupererAvecID($this->req->id);
				}
				catch (MyException $e) {
					try {
						$type_etablissement = TypeEtablissement::recupererAvecID($this->session->modification_type_etablissement_id);
					}
					catch (MyException $e) {
						$this->site->redirect('admtypeetablissement');
					}
				}
				
				if (isset($this->session->modification_type_etablissement_id)) {
					$this->req->id = $this->session->modification_type_etablissement_id;
					unset($this->session->modification_type_etablissement_id);
				}
			
				$formulaire = new Form("?module=admtypeetablissement&action=valider_modification","formulaire");
				$formulaire->add_text("type_etablissement","type_etablissement","Détails sur le type :")->set_value($type_etablissement->valeur);
				$formulaire->add_submit("modifier_type_etablissement_submit", "modifier_type_etablissement_submit")->set_value("Modifier le type");	
				$this->session->modification_type_etablissement_id = $this->req->id;
				
				$this->tpl->assign("formulaire", $formulaire);
				$this->tpl->assign("code", $type_etablissement->identifiant);
			}
			else {
				$this->site->redirect('admtypeetablissement');
			}
		}
		
		public function action_valider_modification() {
			if ($this->req->type_etablissement == '') {
				$this->site->ajouter_message('Vous ne pouvez pas donner un type vide !');
				$this->site->redirect('admtypeetablissement', 'modifier');
			}	
			else {
				unset($this->req->modification_type_etablissement_id);
				
				try {
					$type_etablissement = TypeEtablissement::recupererAvecID($this->session->modification_type_etablissement_id);
				}
				catch (MyException $e) {
				}
				
				$type_etablissement->valeur = $this->req->type_etablissement;
				
				$type_etablissement->enregistrer();
				
				$this->site->ajouter_message('Modification effectuée');
				$this->site->redirect('admtypeetablissement');
			}
		}
		
		public function action_supprimer() {
			if ($this->req->id != '') {
				$type_etablissement = TypeEtablissement::recupererAvecID($this->req->id);
				$type_etablissement->supprimer();
				
				$this->site->ajouter_message('Suppression effectuée');
				$this->site->redirect('admtypeetablissement');
			}
			else {
				$this->site->redirect('admtypeetablissement');
			}
		}
	}
?>